package com.example.mybiadushoujiweishi.activity.acivitys.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.example.mybiadushoujiweishi.activity.acivitys.entity.Versioninfo;
import com.example.mybiadushoujiweishi.activity.acivitys.services.MyService;
import com.example.mybiadushoujiweishi.activity.acivitys.services.VersionUpdate;
import com.example.mybiadushoujiweishi.activity.acivitys.utils.MyApplication;
import com.socks.library.KLog;




import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/5/22 0022.
 */

public class CheckUpdateBroadcast extends BroadcastReceiver {

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            System.out.println(msg.obj.toString());
           String ms= msg.obj.toString();
           /* BanbenUpdateActivity banbenUpdateActivity=new BanbenUpdateActivity();
            banbenUpdateActivity.dialogshow(ms);*/
        }
    };


    //广播接收
    @Override
    public void onReceive(Context context, final Intent intent) {
        KLog.i("======", "我准备好了");

        SharedPreferences sharedPreferences = MyApplication.getSharedPreferences();
        String olddata = sharedPreferences.getString("checkDate", null);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String newdateformat=sdf.format(date);
        System.out.println(newdateformat+"  }}}}}}}}}}}}}}}}}");
    //    sdf.format() 由日期转换成String
        // sdf.parse() 将string转成日期类

        //计算日期差

        try {
            Date checkdate = null;
            if (olddata!=null) {
               checkdate = sdf.parse(olddata);
            }
            if (olddata==null||"".equals(olddata))
            {
                //表示是第一次安装 将当前的日期存储在共享首选项
            SharedPreferences.Editor editor=sharedPreferences.edit();
                editor.putString("checkDate",newdateformat);
                editor.commit();

            }
            else {
                int days=CheckUpdateBroadcast.daysBetween(checkdate,date);
                Toast.makeText(context, days+"", Toast.LENGTH_SHORT).show();
                if (days>=7)
                {

                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl("http://10.0.2.2:8080/")
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();
                    VersionUpdate versionUpdate = retrofit.create(VersionUpdate.class);
                    Call<Versioninfo> versionlist = versionUpdate.getversion();
                    versionlist.enqueue(new Callback<Versioninfo>() {
                        @Override
                        public void onResponse(Call<Versioninfo> call, Response<Versioninfo> response) {
                            Versioninfo versioninfo=response.body();
                            int newversioncode=versioninfo.getVersioncode();
                            int oldversioncode=MyApplication.getVersionCode(MyApplication.getContext());
                           if (newversioncode>oldversioncode)
                           {
                             String  apkinfo=versioninfo.getApkinfo();
                               System.out.println(apkinfo);
                               Intent intentone=new Intent();
                               intentone.setClass(MyApplication.getContext(), MyService.class);
                               MyService myService=new MyService();
                               myService.createNotification();
                              intentone.putExtra("apkurl",versioninfo.getApkurl());
                               MyApplication.getContext().startService(intentone);
                             /*  File file = Myuitl.getDir();
                             myService.downloadfile(versioninfo.getApkurl(), file);*/


                           }
                        }

                        @Override
                        public void onFailure(Call<Versioninfo> call, Throwable t) {

                        }
                    });

                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        };




    }



    public static int daysBetween(Date smdate,Date bdate) throws ParseException
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        smdate=sdf.parse(sdf.format(smdate));
        bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }

}
